import { acceptHMRUpdate, defineStore } from 'pinia'
import {CommonGetTempTypeList, CommonGetUserList} from "~/requests";

const cacheKey = "__useTempTypeStore"
export const useTempTypeStore = defineStore('temp_type', () => {
  const options = ref<SelectProps['options']>([]);

  const loadData = async () => {
    let response = await CommonGetTempTypeList();
    let data = response.data?.data || {};
    options.value = Object.keys(data).map((key) => {
      let item = data[key];
      return {
        value: key,
        label: item,
      }
    })
    storeCache();
  }

  const storeCache = () => {
    if (options.value.length) {
      localStorage.setItem(cacheKey, JSON.stringify(options.value));
    }
  }
  const restoreCache = () => {
    const data = localStorage.getItem(cacheKey);
    if (data) {
      options.value = JSON.parse(data);
    }else {
      loadData();
    }
  }

  onMounted(() => {
    restoreCache();
  })

  return {
    options,
    loadData,
  }
})

if (import.meta.hot)
  import.meta.hot.accept(acceptHMRUpdate(useTempTypeStore as any, import.meta.hot))
